package com.cloud.bc.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.cloud.bc.config.WXPayConfigImpl;
import com.cloud.bc.model.entity.BcWechatemplateMessages;
import com.cloud.bc.service.BcWechatemplateMessagesService;
import com.cloud.bc.service.CjVxiangqinService;
import com.cloud.bc.util.BcEmailUtil;
import com.cloud.bc.util.BcSmsUtil;
import com.cloud.common.model.enums.MailTemplatesEnum;
import com.cloud.common.model.enums.SmsTemplatesEnum;
import com.cloud.common.util.EmailUtil;
import com.cloud.common.util.R;
//import com.github.wxpay.sdk.WXPay;
//import com.github.wxpay.sdk.WXPayConfig;
import com.cloud.wx.config.WeiXinPayConfig;
import com.cloud.wx.model.request.TemplateData;
import com.cloud.wx.model.request.TemplateMessageRequest;
import com.cloud.wx.model.response.TemplateMessageResponse;
import com.cloud.wx.pay.TestUtil;
import com.cloud.wx.pay.WXPay;
import com.cloud.wx.template.WxMessageService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里OSS内网映射
 * https://www.cnblogs.com/-mrl/p/10691271.html
 */
@Controller
@AllArgsConstructor
@RequestMapping("test")
public class TestContorller {
    private final WeiXinPayConfig weiXinPayConfig;
    private final BcEmailUtil bcEmailUtil;
    private final BcSmsUtil bcSmsUtil;
    private final WxMessageService wxMessageService;
    private final CjVxiangqinService cjVxiangqinService;
    @ResponseBody
    @RequestMapping("cjvxxingqin")
    public R cjvxxingqin(Integer maxPage){
        cjVxiangqinService.main(maxPage);
        return new R();
    }
    @SneakyThrows
    @ResponseBody
    @RequestMapping("wxPaySanboxKey")
    public R wxPaySanboxKey() {
        TestUtil testUtil = new TestUtil();
        WXPayConfigImpl wxPayConfig = new WXPayConfigImpl(weiXinPayConfig);
//        获取微信商户沙箱key
        String sandboxSignKey = testUtil.getSandboxSignKey(wxPayConfig);
        return new R(sandboxSignKey);
    }

    @SneakyThrows
    @ResponseBody
    @RequestMapping("test")
    public R test() {
        WXPayConfigImpl wxPayConfig = new WXPayConfigImpl(weiXinPayConfig);
//        https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
//        调起支付页面
        WXPay wxpay = new WXPay(wxPayConfig, true, true);
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "腾讯充值中心-QQ会员充值");
        data.put("out_trade_no", "2016090910595900000012");
//        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", "101");
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        //手机端用JSAPI,电脑端用Native
        data.put("trade_type", "JSAPI");  // 此处指定为扫码支付
        data.put("product_id", "12");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new R();
    }

    @SneakyThrows
    @ResponseBody
    @RequestMapping("mail")
    public R mail() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "123456");
        bcEmailUtil.sendEmail(MailTemplatesEnum.REG_CODE, "dou631@163.com", null);
        return new R();
    }

    @SneakyThrows
    @ResponseBody
    @RequestMapping("sms")
    public R sms() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "123456");
        bcSmsUtil.sendSms(SmsTemplatesEnum.REG_CODE, "13426401627", jsonObject);
        return new R();
    }

    @SneakyThrows
    @ResponseBody
    @RequestMapping("wx/template")
    public R wxTemplate() {
        /** 增加配置
         *        {{first.DATA}} {{keyword1.DATA}} {{keyword2.DATA}} {{keyword3.DATA}} {{keyword4.DATA}} {{keyword5.DATA}} {{remark.DATA}}
         * */
        String time = DateUtil.today();
        String remark = "备注";
        String content = "content";
        String url = "http://www.xiats.com";
        Map<String, Object> map = new HashMap<String, Object>();
        String templateId = "dRjUTZgZ-AqoGVmyi-Xk4AHa-HMp5-2G1eJqQAg7_WE";//模板id
        String openId = "oPrXE6EosL-sHDDnNHyPL9WiwCyA";//通知给谁
        String json = TemplateData.New()
                .setTouser(openId)//hm.gao
                .setTemplate_id(templateId)
                //.setTopcolor("#743A3A")
                .setUrl(url)
                .add("first", "有新的采购信息", "#000000")
                .add("keyword1", content, "#00b0f0")
                .add("keyword2", time, "#000000")
                .add("remark", remark, "#00b0f0")
                .build();
        TemplateMessageResponse b = wxMessageService.sendTemplateMessageData(json);
        return new R(b);
    }

}
